package com.bkhech.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import com.bkhech.RedisKeyConstant;
import com.bkhech.api.feign.FileFein;
import com.bkhech.enums.PlatformEnum;
import com.bkhech.excptions.GraceException;
import com.bkhech.grace.result.GraceJSONResult;
import com.bkhech.grace.result.ResponseStatusEnum;
import com.bkhech.mapper.UsersMapper;
import com.bkhech.pojo.Users;
import com.bkhech.pojo.bo.ModifyUserBO;
import com.bkhech.pojo.vo.UsersVO;
import com.bkhech.utils.RedisOperator;
import com.bkhech.utils.UserTokenUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * UsersService
 * 用户表 服务实现类
 *
 * @author guowm
 * @date 2024-11-06
 */
@Service
public class UsersService {

    @Resource
    private UsersMapper usersMapper;

    @Resource
    private RedisOperator redisOperator;

    @Resource
    private FileFein fileFein;

    public void modifyUserinfo(ModifyUserBO modifyUserBO) {
        Users users = new Users();

        String userId = modifyUserBO.getUserId();
        String wechatNum = modifyUserBO.getWechatNum();
        if (StringUtils.isBlank(userId)) {
            GraceException.display(ResponseStatusEnum.USER_INFO_UPDATED_ERROR);
        }
        if (StringUtils.isNotBlank(wechatNum)) {
            String isExist = redisOperator.get(RedisKeyConstant.USER_ALREADY_UPDATE_WECHAT_NUM + userId);
            if (StringUtils.isNotBlank(isExist)) {
                GraceException.display(ResponseStatusEnum.WECHAT_NUM_ALREADY_MODIFIED_ERROR);
            } else {
                // 修改微信二维码
                String qrCodeUrl = getQrCodeUrl(wechatNum, userId);
                users.setWechatNumImg(qrCodeUrl);
            }
        }
        users.setId(userId);
        users.setUpdatedTime(LocalDateTime.now());

        BeanUtils.copyProperties(modifyUserBO, users);
        usersMapper.updateById(users);

        if (StringUtils.isNotBlank(wechatNum)) {
            redisOperator.setByDays(RedisKeyConstant.USER_ALREADY_UPDATE_WECHAT_NUM + userId, userId, 365);
        }

    }


    private String getQrCodeUrl(String wechatNum, String userId) {
        GraceJSONResult<String> result = fileFein.generatorQrCode(wechatNum, userId);
        String qrCodeUrl = result.getData();
        return qrCodeUrl;
    }


    public UsersVO getUserInfoById(String userId, boolean needToken) {
        Users users = usersMapper.selectById(userId);
        UsersVO usersVO = new UsersVO();
        BeanUtils.copyProperties(users, usersVO);
        if (needToken) {
            String userToken = UserTokenUtil.setToken(userId, PlatformEnum.TOKEN_APP_PREFIX);
            usersVO.setUserToken(userToken);
        }
        return usersVO;
    }

    /**
     * 根据手机号或者微信号查询微信号
     *
     * @param queryString
     * @return
     */
    public Users getByWechatNumOrMobile(String queryString) {
        QueryWrapper<Users> usersQueryWrapper = new QueryWrapper<>();
        usersQueryWrapper.lambda().eq(Users::getWechatNum, queryString)
                .or().eq(Users::getMobile, queryString);
        return usersMapper.selectOne(usersQueryWrapper);
    }

}
